package DAO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import Classes.Filme;
import Nucleo.ConexaoBanco;

public class FilmeDAO {

	private ConexaoBanco conn;
	
	public FilmeDAO(){
		conn = new ConexaoBanco();
	}
	
	public void inserir(Filme filme) {
		String sql = "INSERT INTO `Filme` (nome,resumo) values ( ? , ?) ";
		try {
			PreparedStatement stmt = conn.getConnection().prepareStatement(sql);
			stmt.setString(1, filme.getNome()   );
			stmt.setString(2, filme.getResumo() );
			stmt.execute();
			stmt.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public List<Filme> getFilmes(){
		String sql = "SELECT * FROM `Filme`";
		List<Filme> lista = new ArrayList<Filme>();
		
		try {
			PreparedStatement stmt = conn.getConnection().prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			
			//SimpleDateFormat formatarDate = new SimpleDateFormat("yyyy-MM-dd");
			
			while(rs.next()) {
				Filme f = new Filme();
				
				f.setId		( rs.getInt("idFilme") );
				f.setNome	( rs.getString("nome") );
				f.setResumo ( rs.getString("resumo") );
								
				lista.add(f);
			}
			
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return lista;
	}

	public Filme getFilmePeloId( int idFilme ){
		Filme f = null;
		
		String sql = "SELECT * FROM `Filme` WHERE `idFilme` = '"+ idFilme +"'";
		
		try {
			PreparedStatement stmt = conn.getConnection().prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			
			while(rs.next()) {
				f = new Filme();
				
				f.setId		( rs.getInt("idFilme") );
				f.setNome	( rs.getString("nome") );
				f.setResumo ( rs.getString("resumo") );
				
			}
			
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return f;
		
	}

	public boolean deletar(int idFilme) {
		String sql = "DELETE FROM `filme` WHERE `idFilme` = '"+ idFilme+"'";
		
		try {
			Statement stmt = conn.getConnection().createStatement();
			int delete = stmt.executeUpdate(sql);
			if ( delete == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
		
	}
	
	
}

